<?php
class contentModel extends baseModel{
    protected $table = 'content';

    //列表公共信息
    public function common_list()
    {
        $data['category_list']=model('category')->category_list();
        return $data;
    }

    //公共列表筛选
    public function common_list_where()
    {
        //排序
        $sequence=intval($_POST['sequence']);
        switch ($sequence) {
            case '1':
                $order='A.updatetime DESC';
                break;
            case '2':
                $order='A.updatetime ASC';
                break;
            case '3':
                $order='A.aid DESC';
                break;
            case '4':
                $order='A.aid ASC';
                break;
            case '5':
                $order='A.inputtime DESC';
                break;
            case '6':
                $order='A.inputtime ASC';
                break;
            case '7':
                $order='A.views DESC';
                break;
            case '8':
                $order='A.views ASC';
                break;
        }
        if(!empty($order)){
        $order=$order.',';
        }

        //状态
        $status=intval($_POST['status']);
        switch ($status) {
            case '1':
                $where[]=' AND A.status=1';
                break;
            case '2':
                $where[]=' AND A.status=0';
                break;
        }

        //搜索
        $search=in(urldecode($_POST['keyword']));
        if(!is_utf8($search)){
            $search=auto_charset($search);
        }
        if(!empty($search)){
        $where[]=' AND A.title like "%' . $search . '%"';
        }

        //栏目ID
        $cid=intval($_POST['cid']);
        if(!empty($cid)){
            $where[]=' AND A.cid='.$cid;
        }

        $str ='';
        if(!empty($where)){
            foreach ($where as $value) {
                $str .= $value;
            }
        }
        return array(
            'order'=>$order,
            'where'=>$str
        );

    }

    //公共调用信息
    public function common_info($id)
    {
        if($id){
            $info=$this->info($id);
            $file_id=api('editor','get_relation',array('table'=>'content','id'=>$id));
            $cid=$info['cid'];
            $action_name='编辑';
            $action='edit';
            $category_info = model('category')->info($cid);
        }else{
            $file_id=',';
            $action_name='添加';
            $action='add';
        }
        
        $category_list = model('category')->category_list();
        $tpl_list=model('setting')->tpl_list();

        $data['info']=$info;
        $data['info_data']=$info_data;
        
        $data['file_id']=$file_id;
        $data['action_name']=$action_name;
        $data['action']=$action;
        $data['category_info']=$category_info;
        $data['category_list']=$category_list;
        $data['tpl_list']=$tpl_list;

        return $data;
    }

    //公共保存检测信息
    public function common_data_check($data)
    {
        if(empty($data['cid'])){
            $this->msg('未选择任何栏目！',0);
        }
        ApiHook('hook_admin_content_data_check',$data);
    }

    //获取内容基本信息
    public function info($aid)
    {
        return $this->find('aid='.$aid);
    }

    //字段保存格式化
    public function common_data_save($data)
    {
        $data['updatetime']=strtotime($data['updatetime']);
        if(empty($data['updatetime'])){
            $data['updatetime']=time();
        }
        if(is_array($data['recommend'])){
            $data['recommend']=implode(',',$data['recommend']);
        }else{
			$data['recommend']='';
		}
        $data['urltitle']=$this->get_urltitle($data['title'],$data['urltitle'],$data['aid']);
        $data['taglink']=intval($data['taglink']);
        //提取缩略图
        $data['tool_image']=intval($data['tool_image']);
        if(empty($data['image'])&&$data['tool_image']&&$data['content']){
            preg_match_all('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i',html_out(substr($data['content'],0,10000)),$match);
            $data['image']=$match[1][intval($data['tool_image'])-1];
        }
        //提取描述
        $data['tool_description']=intval($data['tool_description']);
        if(empty($data['description'])&&$data['tool_description']&&$data['content']){
            if($data['tool_description']>250){
                $data['tool_description']=250;
            }
            $description=substr($data['content'], 0,10000);
            $description=html_out($description);
            $description=strip_tags($description);
            $description=str_replace(array("\r\n","\t",'&ldquo;','&rdquo;','&nbsp;'), '', $description);
            $data['description']=substr($description, 0,$data['tool_description']);
        }
        return $data;
    }


    //基础表信息
    public function add_save($data)
    {
        //格式化部分字段
        $data=$this->common_data_save($data);
        $data['inputtime']=time();
        //录入数据
        $aid=$this->insert($data); //录入基本信息
        $data['aid']=$aid;
        api('recommend','content_save',array('str'=>$data['recommend'],'aid'=>$aid)); //关联推荐位
        api('tags','content_save',array('keywords'=>$data['keywords'],'aid'=>$aid)); //关联TAG
        api('editor','relation',array('table'=>'content','file_id'=>$data['file_id'],'id'=>$aid));
        ApiHook('hook_admin_content_save_data',$data);
        return $aid;
    }


    //基础表信息
    public function edit_save($data,$ext=true)
    {
        //格式化部分字段
        $data=$this->common_data_save($data);
        //录入数据
        $this->update('aid='.$data['aid'], $data); //录入基本信息
        api('recommend','content_save',array('str'=>$data['recommend'],'aid'=>$data['aid'])); //关联推荐位
        api('tags','content_save',array('keywords'=>$data['keywords'],'aid'=>$data['aid'])); //关联TAG
        api('editor','relation',array('table'=>'content','file_id'=>$data['file_id'],'id'=>$data['aid']));
        ApiHook('hook_admin_content_save_data',$data);
        return $aid;
    }

    //内容删除
    public function del($aid)
    {
        //删除内容基本信息
        ApiHook('hook_admin_content_del_top_data',$aid);
        $status=$this->delete('aid='.$aid); 
        api('recommend','content_del',$aid); //删除推荐位
        api('tags','content_del',$aid); //删除TAG关联
        api('editor','del_file',array('table'=>'content','id'=>$aid));
        ApiHook('hook_admin_content_del_foot_data',$aid);
        return $status;
    }

    //获取内容ID列表
    public function get_list_id($cid)
    {
        return $this->select('cid='.$cid,'aid');
    }

    //审核草稿
    public function status($aid,$status){
        $data['status']=intval($status);
        $this->update('aid='.$aid, $data);
    }

    //修改栏目ID
    public function edit_cid($aid,$cid){
        $data['cid']=intval($cid);
        $this->update('aid='.$aid, $data);
    }

    //获取标题拼音
    public function get_urltitle($name='', $urlname = null, $aid = null)
    {
        if(empty($name)){
            return false;
            exit;
        }
        if (empty($urlname))
        {
            $pinyin = new Pinyin();
            $name = preg_replace('/\s+/', '-', $name);
            $pattern = '/[^\x{4e00}-\x{9fa5}\d\w\-]+/u';
            $name = preg_replace($pattern, '', $name);
            $urlname = substr($pinyin->output($name, true),0,30);
            if(substr($urlname,0,1)=='-'){
                $urlname=substr($urlname,1);
            }
            if(substr($urlname,-1)=='-'){
                $urlname=substr($urlname,0,-1);
            }
        }

        $where='';
        if (!empty($aid))
        {
            $where = 'AND aid<>' . $aid;
        }
        
        $info = $this->model->table('content')->where("urltitle='".$urlname."'" .$where)->count(); 

        if (empty($info))
        {
            return $urlname;
        }
        else
        {
            return $urlname.substr(cp_uniqid(),8);
        }
    }

    //获取关键词
    public function get_keyword($title,$content){
         $data=Http::doGet('http://keyword.discuz.com/related_kw.html?ics=utf-8&ocs=utf-8&title='.urlencode($title).'&content='.urlencode($content),10);
        if(empty($data)){
            return;
        }
        preg_match_all("/<kw>(.*)A\[(.*)\]\](.*)><\/kw>/",$data, $list, PREG_SET_ORDER);

        if(empty($list)){
            return;
        }

        $keywords='';
        foreach ($list as $value) {
            $keywords.=$value[2].',';
        }
        return substr($keywords,0,-1);
        

    }

    //获取所有内容统计
    public function content_count()
    {
        return $this->count();
    }





}

?>